/*
 * Copyright 2013-2019 Xia Jun(3979434@qq.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 ***************************************************************************************
 *                                                                                     *
 *                        Website : http://www.farsunset.com                           *
 *                                                                                     *
 ***************************************************************************************
 */
package com.workcell.workdata.im.mvc.controller.group;

import com.workcell.workdata.im.annotation.CreateAction;
import com.workcell.workdata.im.annotation.UID;
import com.workcell.workdata.im.entity.Group;
import com.workcell.workdata.im.model.request.GroupCreateRequest;
import com.workcell.workdata.im.model.request.GroupInviteRequest;
import com.workcell.workdata.im.model.response.ResponseEntity;
import com.workcell.workdata.im.model.vo.GroupVO;
import com.workcell.workdata.im.service.GroupService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/group")
@Api(produces = "application/json", tags = "群组相关接口" )
@Validated
public class GroupController {

	@Resource
	private GroupService groupService;

	@ApiOperation(httpMethod = "GET", value = "获取我的群列表")
	@GetMapping(value = "/list")
	public ResponseEntity<List<GroupVO>> list(@ApiIgnore @UID Long uid) {

		return ResponseEntity.ok(groupService.getRelevantList(uid));
	}

	@ApiOperation(httpMethod = "GET", value = "获取群组详情")
	@ApiImplicitParam(name = "id", value = "群组ID", paramType = "path",  dataTypeClass = Long.class,example = "0")
	@GetMapping(value = "/{id}")
	public ResponseEntity<GroupVO> get(@PathVariable long id) {

		return ResponseEntity.ok(groupService.findOne(id));
	}

	@ApiOperation(httpMethod = "POST", value = "创建群组")
	@PostMapping(value = "")
	@Validated(CreateAction.class)
	public ResponseEntity<Long> create(@Valid @RequestBody GroupCreateRequest request, @ApiIgnore @UID Long uid) {

		Group group = groupService.create(request,uid);

		return ResponseEntity.ok(group.getId());
	}

	@ApiOperation(httpMethod = "PATCH", value = "修改群组名称")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "群组ID", paramType = "path",  dataTypeClass = Long.class,example = "0"),
			@ApiImplicitParam(name = "name", value = "名称", paramType = "query", dataTypeClass = String.class),
	})
	@PatchMapping(value = "/{id}/name")
	public ResponseEntity<Void> updateName(@PathVariable Long id, @RequestParam String  name) {

		groupService.updateName(id, name);

		return ResponseEntity.ok();
	}


	@ApiOperation(httpMethod = "DELETE", value = "解散群组")
	@ApiImplicitParam(name = "id", value = "群组ID", paramType = "path",  dataTypeClass = Long.class,example = "0")
	@DeleteMapping(value = "/{id}")
	public ResponseEntity<Void> disband(@PathVariable long id) {
		groupService.delete(id);
		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "POST", value = "群组禁言")
	@ApiImplicitParam(name = "id", value = "群组ID", paramType = "path",  dataTypeClass = Long.class,example = "0")
	@PostMapping(value = "/block/{id}")
	public ResponseEntity<Void> block(@PathVariable Long id) {

		groupService.block(id);
		return ResponseEntity.ok();
	}
	@ApiOperation(httpMethod = "DELETE", value = "解除群组禁言")
	@ApiImplicitParam(name = "id", value = "群组ID", paramType = "path",  dataTypeClass = Long.class,example = "0")
	@DeleteMapping(value = "/block/{id}")
	public ResponseEntity<Void> unblock(@PathVariable Long id) {

		groupService.unblock(id);

		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "POST", value = "转让群组")
	@ApiImplicitParams({
			@ApiImplicitParam(name = "id", value = "群组ID", paramType = "path",  dataTypeClass = Long.class,example = "0"),
			@ApiImplicitParam(name = "uid", value = "新群主ID", paramType = "query", dataTypeClass = Long.class,example = "0")
	})
	@PostMapping(value = "/transfer/{id}")
	public ResponseEntity<Void> transfer(@PathVariable Long id , @RequestParam long uid) {

		groupService.transfer(id,uid);

		return ResponseEntity.ok();
	}

	@ApiOperation(httpMethod = "POST", value = "邀请用户入群")
	@PostMapping(value = "/invite")
	@Validated(CreateAction.class)
	public ResponseEntity<Void> invite(@Valid @RequestBody GroupInviteRequest request, @ApiIgnore @UID Long uid ) {

		groupService.invite(request,uid);

		return ResponseEntity.ok();
	}



	@ApiOperation(httpMethod = "POST", value = "群组信息修改")
	@PostMapping(value = "/photo")
	public ResponseEntity<Void> block(@RequestBody Group group) {
		groupService.updateGroupPhoto(group);
		return ResponseEntity.ok();
	}
}
